package com.acwing.partition11;

import java.io.*;

/**
 * @author `RKC`
 * @date 2022/3/3 8:40
 */
public class AC1055股票买卖II_dp {

    private static final int N = 100010, INF = 0x3f3f3f3f;
    private static int[] w = new int[N];
    //f[i][0]表示考虑前i天，且i-1天时不持股的最大收益；f[i][1]表示考虑前i天，且i-1天时持股的最大收益
    private static int[][] f = new int[N][2];
    private static int n;

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        n = Integer.parseInt(reader.readLine());
        String[] ss = reader.readLine().split(" ");
        for (int i = 1; i <= n; i++) w[i] = Integer.parseInt(ss[i - 1]);
        f[0][0] = 0;
        f[0][1] = -INF;
        for (int i = 1; i <= n; i++) {
            f[i][0] = Math.max(f[i - 1][0], f[i - 1][1] + w[i]);
            f[i][1] = Math.max(f[i - 1][1], f[i - 1][0] - w[i]);
        }
        writer.write(f[n][0] + "\n");
        writer.flush();
    }
}
